<template>
    <div>
        <div class="blog-up">
            <p class="blog-up-name">如何生成文章目录</p>
        </div>
        <div class="show-content">
            <div class="show-left">
                <h3><span>文章目录</span><i class="el-icon-close"></i></h3>
                <ul>
                    <li class="star"><i class="el-icon-star-off"></i>&nbsp;&nbsp;<a href="#need">实现的需求</a></li>
                    <li class="star"><i class="el-icon-star-off"></i>&nbsp;&nbsp;<a href="#catalogue">如何生成文章目录</a></li>
                    <li class="liststar"><i class="el-icon-star-on"></i>&nbsp;&nbsp;<a href="#again">重写原本marked解析h标签的格式</a></li>
                    <li class="liststar lib"><i class="el-icon-star-on"></i>&nbsp;&nbsp;<a href="#get">获取所有h标签生成目录数组</a></li>
                </ul>

               
            </div>
            <div class="show-right">
                <h1>如何生成文章目录</h1>
                <div class="times"><i class="el-icon-time">2020年9月24日</i><i class="el-icon-chat-dot-round" style="margin-left: 30px;">0</i></div>
                <div class="finishs">
                    <h2><a name="need">实现的需求</a></h2>
                    <p>1.点击目录对应标题后， 会自动跳到相应的锚点</p>
                    <p>2.不同层次的标题目录左边距不同</p>
                    <p>3.向下滚动后一段距离后，目录相对屏幕固定</p>
                </div>
                <div class="finishs">
                    <h2><a name="catalogue">如何生成文章目录</a></h2>
                </div>
                <div class="processs">
                    <h3><a name="again"></a>重写原本marked解析h标签的格式</h3>
                    <p>原本marked解析h标签会将除了英文和数字以外的字符替换为'-'，包括中文字符，如果用中文写h标签，这样生成的锚点和id都为'-'字符，除非用英文数字写标题，所以需要对h标签的格式重写。</p>
                    <p>通过设置renderer.heading后,  marked解析h标签的格式可以重写。具体可以看src/components/back/editor.vue</p>
                    <pre><code class="lang-html"><span class="hljs-tag">&lt;<span class="hljs-name">h1</span>&gt;</span>一级标题<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>

<span class="hljs-comment">&lt;!--转换为下面这种格式--&gt;</span>

<span class="hljs-tag">&lt;<span class="hljs-name">a</span> <span class="hljs-attr">href</span>=<span class="hljs-string">"#一级标题"</span> <span class="hljs-attr">class</span>=<span class="hljs-string">"hashTitle"</span> <span class="hljs-attr">data-scroll</span>&gt;</span>
    <span class="hljs-tag">&lt;<span class="hljs-name">h1</span> <span class="hljs-attr">id</span>=<span class="hljs-string">"一级标题"</span>&gt;</span>一级标题<span class="hljs-tag">&lt;/<span class="hljs-name">h1</span>&gt;</span>
<span class="hljs-tag">&lt;/<span class="hljs-name">a</span>&gt;</span>
</code></pre>
                    <p>上面的data-scroll是使smooth-scroll，可以是跳转锚点平滑滚动</p>
                </div>
                <div class="processs">
                    <h3><a name="get">获取所有h标签生成目录数组</a></h3>
                    <p>首先将从后台获取到的内容用marked解析成html字符串，再用正则表达式取出h标签中的重要信息，h标签的等级和id值，然后通过一点简单的算法算出不同标签对应的层级， 不同的层级在目录里的左边距不同，最后push到目录list数组中。下面是目录数组的格式。</p>
                    <pre><code class="lang-js"><span class="hljs-string">let</span> <span class="hljs-string">list</span> <span class="hljs-string">=</span> <span class="hljs-string">[</span>
    <span class="hljs-string">{</span>
        <span class="hljs-attr">level:</span> <span class="hljs-number">1</span><span class="hljs-string">,</span>
        <span class="hljs-attr">size:</span> <span class="hljs-number">2</span><span class="hljs-string">,</span>
        <span class="hljs-attr">content:</span> <span class="hljs-string">'二级标题'</span>
    <span class="hljs-string">},</span>
    <span class="hljs-string">{</span>
        <span class="hljs-attr">level:</span> <span class="hljs-number">2</span><span class="hljs-string">,</span>
        <span class="hljs-attr">size:</span> <span class="hljs-number">3</span><span class="hljs-string">,</span>
        <span class="hljs-attr">content:</span> <span class="hljs-string">'三级标题'</span>
    <span class="hljs-string">}</span>
<span class="hljs-string">];</span>
</code></pre>
                    <p>具体的算法可以看src/store/getters.js</p>
                </div>
                <div>
                    <Email></Email>
                </div>
            </div>
        </div>
        
    </div>
</template>

<script>
import Email from '@/components/email'

export default {
    name:'show',
    components:{
        Email,
    }

}
</script>

<style scoped>
    *{
        margin: 0;
        padding: 0;
    }
    a{
        text-decoration: none;
        color: #fff;
    }

    .blog-up{
        background:url('../assets/sunset.jpg');
        height:570px;
        width:100%;
        background-size: 100% 100%;
        display: flex;
        align-items: center;
        justify-content: center;
    }
    .blog-up-name{
        margin: 0;
        color: #fff;
        font-size: 45px;
        font-family: 'Times New Roman';
    }
    .show-content{
        display: flex;
        background-color: black;
        color: #fff;
    }
    .show-left{
        width: 300px;
        text-align: left;
        margin-left: 32px;
        margin: 40px 20px 20px 10px;
    }
    .show-left h3 span{
        margin-right: 20px;
    }
    .show-left ul{
        color: #fff;
        list-style: none;
    }
    .show-left ul li{
        height: 30px;
        width: 190px;
    }
    .show-left h3{
        margin-bottom: 10px;
    }
    .star{
        margin-left:10px;
    }
    .liststar{
        margin-left: 30px;
        margin-top: 5px;
    }
    .lib{
        margin-top: 20px;
    }
    .show-right{
        width: 800px;
        text-align: left;
    }
    .show-right h1{
        text-align: center;
        margin-top: 70px;
    }
    .times{
        text-align: right;
    }
    .firsts{
        font-size: 25px;
        font-weight: 600;
        border-bottom: 2px solid #fff;
        padding: 20px 0 10px 0;
    }
    .codes{
        margin: 35px 0px 35px 0;
    }
    .codes p{
        margin-top: 10px;
        color: #e3eaf1;
    }
    .processs{

    }
    .processs h3{
        padding: 30px 0 10px 0;
    }
    .finishs{

    }
    .finishs h2{
        border-bottom: 2px solid;
        padding: 50px 0 15px 0;
    }
    .finishs p{
        padding-top: 25px;
    }
    pre{
        padding: 0.625rem 0.625rem;
        background: #474949;
        border-radius: 0.3125rem;
        overflow-x: auto;
        font-size: 18px;
        margin-top: 20px;
    }
    code{
        color: #93f0d8;
    }
    .hljs-comment{
        color: #8e908c;
    }
    .hljs-tag{
        color: darkturquoise;
    }
    .hljs-string{
        color: greenyellow;
    }
    .hljs-attribute {
        color: #eab700;
    }
    .hljs-number{
        color: #f5871f;
    }
    .hljs-keyword{
        color: #cf20ff;
    }
</style>